﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Data;
using System.Security.Principal;
using Microsoft.AspNetCore.Authorization;
using ZhiJiao.Models.Entities;
using ZhiJiao.Service.Dto;
using ZhiJiao.Service.Abstracts;
using ZhiJiao.Models.Entities.Enums;

namespace ZHIJIAO.Controllers
{
    public class ModuleController : Controller
    {
        private readonly IModuleService _moduleService;

        public ModuleController(IModuleService moduleService)
        { 
            _moduleService = moduleService;
        }


        #region 模块管理页面Index
        [Authorize]
        public ActionResult Index()
        {
            //ViewBag.PowersList = JsonConvert.SerializeObject(GetPowersList());
            return View();
        }

        #endregion

        [HttpGet("api/module/get-module-list")]
        public IActionResult GetList() 
        {
            return Json(_moduleService.GetModuleList());
        }

        [HttpGet("api/module/search-module")]
        public IActionResult SearchModule(bool? filterFlag, string? name,ModuleType? type)
        {
            return Json(_moduleService.SearchModule(filterFlag,name,type));
        }

        [HttpPost("api/module/module-submit")]
        public IActionResult AddModule( [FromBody]ModuleDto model) 
        {
            if (model.Id == null)
            {
                return Json(_moduleService.AddModule(model));
            }
            else
            {
                return Json(_moduleService.EditModuleSubmit(model));
            }
        }

        [HttpGet("api/module/delete-module")]
        public IActionResult DeleteModule(long id)
        {
            return Json(_moduleService.DeleteModule(id));
        }


        [HttpGet("api/module/get-parent-modules")]
        public IActionResult GetParentModules(long? id,ModuleType type)
        {
            return Json(_moduleService.GetParentModules(id,type));
        }
    }
}
